﻿using System;
using System.Web;
using System.Data;

namespace msBsc.WebUi
{
    public partial class wfCustCart : msBsc.Cls.BPCustom
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string strsid = this.Request.QueryString.Get("s");
                if (string.IsNullOrEmpty(strsid))
                {
                    strsid = "";
                }
                if (strsid == "0")
                {
                    this.ExecJScript("$('.current').removeClass('current');");
                    this.ExecJScript("$('.switch-cart-2').addClass('current');");
                    this.ExecJScript("$('#J_FilterBar .floater').animate({ left: 134 * 1 + 'px' }, 100);");
                }
                else if (strsid == "1")
                {
                    this.ExecJScript("$('.current').removeClass('current');");
                    this.ExecJScript("$('.switch-cart-4').addClass('current');");
                    this.ExecJScript("$('#J_FilterBar .floater').animate({ left: 134 * 2 + 'px' }, 100);");
                }
                loadlist(strsid, "", "", ""); 
            }
        }

        private void ShowStatic()
        {
            using (msBsc.AppLogic.DataAlc.CBizShpCustomerCart odataobj = new AppLogic.DataAlc.CBizShpCustomerCart())
            {
                DataSet ds = odataobj.SelectCartStatistic(this.oUserInfo.Operator);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    this.ExecJScript("$('#emD1').html('" + ds.Tables[0].Rows[0]["ALL_GOODS"].ToString() + "');");
                    this.ExecJScript("$('#emD2').html('" + ds.Tables[0].Rows[0]["PRICE_DOWN"].ToString() + "');");
                    this.ExecJScript("$('#emD3').html('" + ds.Tables[0].Rows[0]["KUCUN_DOWN"].ToString() + "');"); 
                }
            }
        }

        private void loadlist(string strSid
            ,string CurrPageNum,string strPageRowsCount,string strSort)
        {
            using (msBsc.AppLogic.DataAlc.CBizShpCustomerCart odataobj = new AppLogic.DataAlc.CBizShpCustomerCart())
            {
                DataSet ds = odataobj.DataObj.QueryBySQL(@"
 SELECT A.[CART_ORDER_ID] 
      ,A.[COMM_TENANT_ID]
      ,A.[COMM_TENANT_NME]
      ,A.[COMM_USER_NME]
      ,A.[GOODS_ID]
      ,A.[GOODS_NME]
      ,A.[IMG_URL]
      ,A.[PUB_NORMS_ID]
      ,A.[NORMS_DESC]
      ,A.[GOODS_QTY]
      ,GOODS_OLD_PRICE=A.[GOODS_STRIKE_PRICE] 
      ,B.[GOODS_STRIKE_PRICE] 
      ,SHOP_GOODS_QTY=B.[GOODS_QTY] 
      ,SHOP_AMT=A.[GOODS_QTY] *B.[GOODS_STRIKE_PRICE]
      ,IS_DEL=CASE WHEN (B.PUB_NORMS_ID IS NULL OR B.[GOODS_QTY] <1 OR G.GOODS_STATUS_SID<>'1') THEN '1' ELSE '0' END
  FROM [dbo].[SHP_CUSTOMER_CART] A
  LEFT OUTER JOIN SHP_GOODS_NORMS_PUBLIC B ON A.PUB_NORMS_ID=B.PUB_NORMS_ID  
  LEFT OUTER JOIN SHP_GOODS_INFO G ON A.GOODS_ID=G.GOODS_ID
" + " WHERE A.USER_NME='"+this.oUserInfo.Operator+"' AND ('" + strSid + "'='' OR '" + strSid + "'='0' AND B.GOODS_STRIKE_PRICE<A.GOODS_STRIKE_PRICE OR '" + strSid + "'='1' AND B.GOODS_QTY <5)");

                string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "COMM_TENANT_ID,COMM_TENANT_NME,COMM_USER_NME"
                    , (CurrPageNum==string.Empty?1:int.Parse(CurrPageNum))
                    , (strPageRowsCount == string.Empty ? 20 : int.Parse(strPageRowsCount))
                    , (strSort == string.Empty ? "COMM_TENANT_ID DESC" : strSort));
                this.ExecJScript(@" var data= " + strjsontem1 + @" ; 
                updateTableHtml( data);");
                ShowStatic();
            }
        }
         

        public override void DoAction()
        {
            if (this.strAction == "doFinderBoughtList")
            {
                loadlist(this.GetParamHtValue("GOODS_SID") 
                    , this.GetParamHtValue("NEW_PAGE_NUM")
                    , this.GetParamHtValue("PAGE_ROW_COUNT")
                    , this.GetParamHtValue("PAGE_ROW_SORT"));
            }
            else if (this.strAction == "doClearInvalid")
            {
                using (msBsc.AppLogic.DataAlc.CBizShpCustomerCart objorder = new AppLogic.DataAlc.CBizShpCustomerCart())
                {
                    objorder.DataObj.ExecuteBySQL(@"
    DELETE A
    FROM [dbo].[SHP_CUSTOMER_CART] A
    LEFT OUTER JOIN SHP_GOODS_NORMS_PUBLIC B ON A.PUB_NORMS_ID=B.PUB_NORMS_ID  
    LEFT OUTER JOIN SHP_GOODS_INFO G ON A.GOODS_ID=G.GOODS_ID
    WHERE A.USER_NME='" + this.oUserInfo.Operator + @"'
    AND (B.PUB_NORMS_ID IS NULL OR B.[GOODS_QTY] <1 OR G.GOODS_STATUS_SID<>'1')
");
                    this.ExecJScript("window.location.reload(true);");
                }
            } 
        }
    }
}